package com.situ.mall.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.common.util.PageResult;
import com.situ.mall.mapper.CarouselMapper;
import com.situ.mall.pojo.entity.Carousel;
import com.situ.mall.pojo.query.CarouselQuery;
import com.situ.mall.pojo.vo.ProductVO;
import com.situ.mall.service.ICarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarouselServiceImpl implements ICarouselService {
    @Autowired
    private CarouselMapper carouselMapper;

    @Override
    public PageResult list(CarouselQuery carouselQuery) {
        //使用PageHelper开启分页
        PageHelper.startPage(carouselQuery.getPage(), carouselQuery.getLimit());
        //开启分页之后 LIMIT offset,pageSize 这个代码PageHelper帮我们做了
        List<Carousel> list = carouselMapper.list(carouselQuery);
        //查找总数量PageHelper会自动帮我们完成
        //int totalCount = userMapper.selectTotalCount(userQuery);
        PageInfo pageInfo = new PageInfo(list);
        int totalCount = (int) pageInfo.getTotal();

        return PageResult.ok(totalCount, list);
    }

    @Override
    public void add(Carousel carousel) {
        carouselMapper.insertSelective(carousel);
    }

    @Override
    public Carousel selectById(Integer id) {
        return carouselMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Carousel carousel) {
        carouselMapper.updateByPrimaryKeySelective(carousel);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        carouselMapper.deleteAll(ids);
    }

    @Override
    public void deleteById(Integer id) {
        carouselMapper.deleteByPrimaryKey(id);
    }
}
